var app = getApp()

const qiniu = require("../../utils/qiniuUploader");

Page({
    data: {
        classifyList: [], // 类别数据列表
        classifyIndex: 0, // 当前选中类别
        entrustList: [
            {name: '是',attr: true},
            {name: '否',attr: false}
        ], // 委托售卖
        entrustIndex: 0, // 当前选择委托售卖方式
        daysList: [{
            name: '1天',
            attr: 1
        }, {
            name: '10天',
            attr: 10
        }, {
            name: '20天',
            attr: 20
        }, {
            name: '30天',
            attr: 30
        }], // 委托期限
        daysIndex: 0, // 当前选择委托期限

        classify: '', // 类别id
        entrust: '', // 是否委托
        day: '', // 委托期限
        ratio: 0, // 佣金比例
        title: '', // 藏品标题
        intro: '', // 藏品描述
        price: '', // 藏品价格
        phone: '', // 联系方式
        images: [], // 藏品图片
        files_num: 1, // 当前图片id

        cate_id: "", // id
        isSubmit: false, // 是否已提交

        qiniu_token: "",
        qiniu_link: "",
    },

    // formid
    formSubmit: function(e) {
        console.log(e.detail.formId)
        var formId = e.detail.formId

        var that = this;
        wx.getStorage({
            key: 'user',
            success: function(res) {
                wx.request({
                    url: app.url.requestUrl + '/user/collect_formid',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        formid: formId
                    },
                    success: function(res) {

                    }
                })
            }
        })
    },

    // 获取委托买卖 tabbar 标签 
    getTabbarList() {
        var self = this;
        wx.getStorage({
            key: 'user',
            success: function(res) {
                wx.request({
                    url: app.url.requestUrl + '/goods/cate',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                    },
                    success: function(data) {
                        var list = data.data;
                        if (list.code == 1) {
                            self.setData({
                                classifyList: list.data,
                                classify: list.data[0].id,
                                entrust: self.data.entrustList[0].attr,
                                day: self.data.daysList[0].attr
                            })
                        }
                    }
                })
            },
        })
    },
    inputText(e) {
        var value = e.detail.value;
        var name = e.target.dataset.name;
        if (name == 'ratio') {
            this.setData({
                ratio: value
            });
            return
        }
        if (name == 'title') {
            this.setData({
                title: value
            });
            return
        }
        if (name == 'price') {
            this.setData({
                price: value
            });
            return
        }
        if (name == 'intro') {
            this.setData({
                intro: value
            });
            return
        }
        if (name == 'phone') {
            this.setData({
                phone: value
            });
            return
        }
    },
    // 类别选择
    classifyChange: function(e) {
        this.setData({
            classifyIndex: e.detail.value,
            classify: this.data.classifyList[e.detail.value].id
        })
    },
    // 是否委托选择
    entrustChange: function(e) {
        this.setData({
            entrustIndex: e.detail.value,
            entrust: this.data.entrustList[e.detail.value].attr
        })
    },
    // 委托期限选择
    daysChange: function(e) {
        this.setData({
            daysIndex: e.detail.value,
            day: this.data.daysList[e.detail.value].attr
        })
    },
    // 上传图片
    uploadImg(e) {
        var self = this;
        var images = this.data.images;

        var qiniu_link = this.data.qiniu_link;
        var qiniu_token = this.data.qiniu_token;
        var uploadURL = "https://upload.qiniup.com";

        wx.chooseImage({
            count: 9,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success(res) {
                const tempFilePaths = res.tempFilePaths;
                for (var i = 0; i < tempFilePaths.length; i++) {
                    var file = tempFilePaths[i];
                    console.log(file)
                    var uploader = qiniu.upload(file, (res) => {
                        var url = res.imageURL;
                        var id = res.hash;
                        if (images.length >= 9) {
                            return false;
                        }
                        images.push({
                            id: id,
                            url: url
                        })
                        self.setData({
                            images: images,
                        })
                    }, (error) => {
                        console.log('error: ' + error);
                    }, {
                        uploadURL: uploadURL,
                        domain: qiniu_link,
                        uptoken: qiniu_token,
                    });
                }
            }
        })
    },
    // 删除图片
    deleteImg(e) {
        var self = this;
        var id = e.target.dataset.id;
        console.log(id)
        var img = this.data.images;
        img.map((item, index) => {
            if (item.id == id) {
                img.splice(index, 1)
            }
        })
        this.setData({
            images: img
        })
    },
    // 提交表单
    submit() {
        var data = {};
        var img = this.data.images || [];

        data.cate_id = this.data.classify;
        data.is_entrust = this.data.entrust;
        data.ratio = this.data.ratio;
        data.expire = this.data.day;
        data.name = this.data.title;
        data.start_price = this.data.price;
        data.intro = this.data.intro;
        data.phone = this.data.phone;
        data.imgs = [];

        img.map((item, index) => {
            data.imgs.push(item.url);
        });
        data.imgs = data.imgs.join(",");
        
        if (!data.cate_id) {
            wx.showToast({image: '../../img/warning.png',title: '请选择类别'})
            return false;
        }
        if (data.is_entrust && !data.ratio) {
            wx.showToast({image: '../../img/warning.png',title: '请输入佣金比例'})
            return false;
        }
        if (data.is_entrust && !data.expire) {
            wx.showToast({ image: '../../img/warning.png', title: '请选择委托期限' })
            return false;
        }
        if (!data.is_entrust){  // 如果是不委托 -> 清空 佣金比例 和 委托期限
            data.ratio = '';
            data.expire = '';
        }
        if (!data.name) {
            wx.showToast({image: '../../img/warning.png',title: '请输入藏品标题'})
            return false;
        }
        if (!data.start_price) {
            wx.showToast({image: '../../img/warning.png',title: '请输入藏品价格'})
            return false;
        }
        if (!data.intro) {
            wx.showToast({image: '../../img/warning.png',title: '请输入藏品描述'})
            return false;
        }
        if (!data.phone) {
            wx.showToast({image: '../../img/warning.png',title: '请输入联系方式'})
            return false;
        }
        if (!data.imgs) {
            wx.showToast({image: '../../img/warning.png',title: '请上传商品图片'})
            return false;
        }
        this.submitEntrust(data);
    },
    // 表单填入完成且无错误
    submitEntrust(data) {
        var self = this;
        var isSubmit = this.data.isSubmit;

        if (!isSubmit) {
            this.setData({
                isSubmit: true
            })
            wx.getStorage({
                key: 'user',
                success: function(res) {
                    data.openid = res.data.openid;
                    data.token = res.data.token;
                    wx.request({
                        url: app.url.requestUrl + '/goods/addgoods',
                        method: 'POST',
                        data: data,
                        success: function(res) {
                            if (res.data.code == 1) {
                                wx.showLoading({
                                    title: res.data.msg,
                                })
                                setTimeout(function() {
                                    wx.navigateBack({
                                        delta: 1
                                    })
                                    wx.hideLoading()
                                }, 2000)
                            } else {
                                wx.showToast({
                                    icon: 'none',
                                    title: res.data.msg,
                                })
                                self.setData({
                                    isSubmit: false
                                })
                            }
                        }
                    })
                }
            })
        }
    },
    // 获取七牛所需数据
    getQiniuInfo() {
        var self = this;
        var url = "/upload/token";

        wx.getStorage({
            key: 'user',
            success: function(res) {
                wx.request({
                    url: app.url.requestUrl + url,
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token
                    },
                    success: function(res) {
                        var datas = res.data;
                        if (datas.code == 1) {
                            self.setData({
                                qiniu_token: datas.data.token,
                                qiniu_link: datas.data.url
                            })
                        }
                    }
                })
            }
        })
    },
    onLoad: function(options) {
        console.log(options)
        this.getTabbarList()
        this.getQiniuInfo();
        // this.setData({
        //     cate_id: options.id
        // })
    },
})